package pl.edu.agh.winmon.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import pl.edu.agh.winmon.hibernate.CpuInfo;
import pl.edu.agh.winmon.hibernate.CpuLoad;
import pl.edu.agh.winmon.hibernate.MemoryInfo;
import pl.edu.agh.winmon.hibernate.MemoryStatEntry;
import pl.edu.agh.winmon.hibernate.Process;
import pl.edu.agh.winmon.hibernate.ProcessStatEntry;
import pl.edu.agh.winmon.hibernate.Program;
import pl.edu.agh.winmon.hibernate.Session;
import pl.edu.agh.winmon.hibernate.Turn;

/**
 * Class is responsible for providing Hibernate's {@link SessionFactory}.
 */
@SuppressWarnings("nls")
public class SessionFactoryBuilder {

	private static SessionFactory sessionFactory;

	// TODO [ksobon]: Path to database should be obtained form configuration
	// file.
	static {
		Configuration config = new Configuration();
		config.setProperty("hibernate.dialect", "dialect.SQLiteDialect");
		config.setProperty("hibernate.connection.driver_class",
				"org.sqlite.JDBC");
		config.setProperty("hibernate.connection.url",
				"jdbc:sqlite:D:/test.sqlite");
		config.setProperty("hibernate.connection.username", "");
		config.setProperty("hibernate.connection.password", "");
		config.setProperty("hibernate.connection.autocommit", "true");
		config.setProperty("hibernate.show_sql", "false");
		config.setProperty("hibernate.current_session_context_class", "thread");
		config.setProperty("hibernate.hbm2ddl.auto", "update");

		// ADD Hibernate mapping classes HERE !!!
		config.addAnnotatedClass(Session.class);
		config.addAnnotatedClass(CpuInfo.class);
		config.addAnnotatedClass(Turn.class);
		config.addAnnotatedClass(CpuLoad.class);
		config.addAnnotatedClass(Program.class);
		config.addAnnotatedClass(Process.class);
		config.addAnnotatedClass(ProcessStatEntry.class);
		config.addAnnotatedClass(MemoryInfo.class);
		config.addAnnotatedClass(MemoryStatEntry.class);

		sessionFactory = config.buildSessionFactory();
	}

	/**
	 * Returns Hibernate's {@link SessionFactory}.
	 * 
	 * @return Hibernate's {@link SessionFactory}. Should be used to communicate
	 *         with databse.
	 */
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
